const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin')

var path = require('path');
module.exports = {
  mode: "production",
  entry: {
    main: './main.js'
  },
  output: {
    path: path.join(__dirname, "build"),
    publicPath: './',
    publicPath: 'https://cdn.example.com/assets/',
    // filename: '[name].js'
  },
  externals: [
    {
      // String
      react: 'react',
      // Object
       lodash: {
        commonjs: 'lodash',
        amd: 'lodash',
         root: '_' // indicates global variable
      },
      // Array
      subtract: ['./math', 'subtract']
    },
    // Function
    function (context, request, callback) {
      if (/^yourregex$/.test(request)) {
        return callback(null, 'commonjs ' + request);
      }
      callback();
    },
    // Regex
    /^(jquery|\$)$/i
  ],
  module: {
    rules: [
      {
        test: /\.(png|jpg|gif)$/i,
        use: [
          {
            loader: 'url-loader',
            options: {
              limit: 8192,
            },
          },
        ],
      },
      {
        test: /\.(m?js)|(jsx)$/,
        exclude: /(node_modules|bower_components)/,
        use: {
          loader: 'babel-loader',
          options: {
            presets: ['@babel/preset-env', "@babel/preset-react"],
            plugins: [
              '@babel/plugin-transform-runtime'
            ]
          }
        }
      },
    ]
  },
  // 配置插件
  plugins: [
    new HtmlWebpackPlugin({
      filename: 'index.html', // 目标文件名
      template: './index.html' // 源文件路径
    })
  ]
};